//
//  VC_Course.m
//  yjw-winner
//
//  Created by Kae on 2018/9/5.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_Course.h"
#import "VC_CourseDetail.h"
#import "VedioPlayerManager.h"
#import "VC_CourseComment.h"
#import "VC_CourseAllComment.h"
#import "VC_ClassicSentence.h"

@interface VC_Course () {
    VedioPlayerManager *_manager;
}
@property (nonatomic, strong) UIScrollView *scrollView; // 主scrollView
@property (nonatomic, strong) UIView *playerContentView; // 播放器背景view
@property (nonatomic, strong) UIView *segmentContentView; // 选项框背景view
@property (nonatomic, strong) UIViewController *currentViewController;
@end

@implementation VC_Course
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = MJRefreshColor(242, 242, 242);
    }
    return _scrollView;
}
- (UIView *)playerContentView {
    if (!_playerContentView) {
        _playerContentView = [[UIView alloc] init];
        _playerContentView.backgroundColor = [UIColor cyanColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(15, 24, 35, 35);
        [button setImage:[UIImage imageNamed:@"un_vedio_navigation_back"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_playerContentView addSubview:button];
    }
    return _playerContentView;
}
- (UIView *)segmentContentView {
    if (!_segmentContentView) {
        _segmentContentView = [[UIView alloc] init];
        _segmentContentView.backgroundColor = [UIColor redColor];
    }
    return _segmentContentView;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = MJRefreshColor(242, 242, 242);
    self.scrollView.contentSize = self.view.bounds.size;
    self.playerContentView.frame = CGRectMake(0, 0, self.view.mj_w, self.view.mj_w / 750 * 445);
    self.segmentContentView.frame = CGRectMake(0, self.playerContentView.mj_h, self.view.mj_w, self.scrollView.contentSize.height - self.playerContentView.mj_h);
    [self.scrollView addSubview:self.playerContentView];
    [self.scrollView addSubview:self.segmentContentView];
    [self.view addSubview:self.scrollView];
    
    __weak typeof(self) weakself = self;
    _currentViewController = [[VC_CourseDetail alloc] initWith:^(StudyDataCellButtonClickType type) {
        [weakself studyDataViewCallBackButtonClick:type];
    }];
    [self addChildViewController:_currentViewController];
    [_currentViewController didMoveToParentViewController:self];
    _currentViewController.view.frame = self.segmentContentView.bounds;
    [self.segmentContentView addSubview:_currentViewController.view];
    [self initPlayerView];
}

/**
 初始化播放器view
 */
- (void)initPlayerView {
    _manager = [VedioPlayerManager manager];
    View_VedioPlayer *playerView = _manager.playerView;
    playerView.frame = CGRectMake(0, 0, self.playerContentView.mj_w, self.playerContentView.mj_h);
    [self.playerContentView addSubview:playerView];
    [self.playerContentView sendSubviewToBack:playerView];
}

#pragma mark --mark EVENT

- (void)backBarButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 studyVC callBack
 
 @param type type
 */
- (void)studyDataViewCallBackButtonClick:(StudyDataCellButtonClickType)type {
    switch (type) {
        case StudyDataCellButtonClickTypeLookupAllCommnet:{
            // 查看全部评价
            [self studyDataLookupAllComment];
            break;
        }
        case StudyDataCellButtonClickTypeMoreSentence: {
            // 更多金句
            [self studyDataMoreSentence];
            break;
        }
        case StudyDataCellButtonClickTypeComment: {
            // 课程评论
            [self studyDataComment];
            break;
        }
        default:
            break;
    }
}

/**
 课程评价
 */
- (void)studyDataComment {
    VC_CourseComment *vc = [[VC_CourseComment alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 查看所有课程评价
 */
- (void)studyDataLookupAllComment {
    VC_CourseAllComment *vc = [[VC_CourseAllComment alloc] init];
    vc.title = @"课程评论";
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 更多金句
 */
- (void)studyDataMoreSentence {
    VC_ClassicSentence *vc = [[VC_ClassicSentence alloc] init];
    vc.title = @"更多金句";
    [self.navigationController pushViewController:vc animated:YES];
}

@end
